#include<iostream>
#include<unistd.h>
#include<time.h>
#include<string>
#include<cstdarg>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
using namespace std;
enum
{
    Debug=0,
    Information,
    Warning,
    Error,
    Fatal,
    Unknown
};



string levelstring(int level)
{
    
    switch (level)
    {
    case 0:
        return "Debug";
    case 1:
        return "Information";
    case 2:
        return "Warning";
    case 3:
        return "Error";
    case 4:
        return "Fatal";
    default:
        return "Unknown";
    }
}

string timestring()
{
    time_t current_time=time(nullptr);
    return ctime(&current_time);
}


void LogMessage(int level,char* format...)
{
    char left[1024];
    char right[1024];
    string str_level(levelstring(level));
    string str_time(timestring());
    snprintf(left,sizeof(left),"level:%s,time:%s,pid:%d,message:",str_level.c_str(),str_time.c_str(),getpid());

    va_list p;
    va_start(p,format);
    vsnprintf(right,sizeof(right),format,p);
    va_end(p);

    string all(left);
    all+=right;
    all+="\n";
    int fd=open("./log.txt",O_WRONLY|O_CREAT|O_APPEND   );
    cout<<"open file successfully";
    write(fd,all.c_str(),all.size());
    close(fd);
}
